<template>
  <div>
    <div class="particles-js-box">
      <div id="particles-js"></div>
    </div>

    <i18n-selector class="i18n-selector">
      <i class="ri-translate" />

    </i18n-selector>
    
    <div id="login-box">

      <div class="login-banner" />
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
        label-position="left">
        <div class="title-container">
          <h3 class="title">{{ titleName }}</h3>
        </div>
        <div>
          <el-form-item prop="account">
            <el-input ref="name" v-model="loginForm.username" :placeholder="$t('app.account')" type="text" tabindex="1"
              autocomplete="on">
              <template #prefix>
                <svg-icon name="user" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input ref="password" v-model="loginForm.password" :type="passwordType" :placeholder="$t('app.password')"
              tabindex="2" autocomplete="on" @keyup.enter="handleLogin">
              <template #prefix>
                <svg-icon name="password" />
              </template>
              <template #suffix>
                <svg-icon :name="passwordType === 'password' ? 'eye' : 'eye-open'" @click="showPassword" />
              </template>
            </el-input>
          </el-form-item>

          
          <el-form-item prop="code">
            <el-input ref="sidentify" v-model="loginForm.code" type="text" placeholder="请输入验证码"
              tabindex="2" autocomplete="on" @keyup.enter="handleLogin" style="width: 40%;">
            </el-input>
            <img :src="imgUrl" class="codeimg" @click="refreshCode()"  style="width: 50%; overflow: hidden; height: 48px;" alt="">
          </el-form-item>


        </div>
        <el-button :loading="loading" type="primary" style="width: 100%;" @click.prevent="handleLogin">{{
            $t("app.login")
        }}</el-button>
      </el-form>
    </div>
    <a href="https://beian.miit.gov.cn/#/Integrated/index" class="index-a">蜀ICP备2021026529号</a>
  </div>
</template>

<script>
//背景动画
import particlesJs from "@/components/particles/particles.js";
import particlesConfig from "@/components/particles/particles.json";

export default {

  name: "Login",
  data() {
    return {
      title: import.meta.env.VITE_APP_TITLE,
      identifycode: '',
      particlesOptions: {
        preset: "firefly",
      },
      loginForm: {
        // username: "alert1",
        // password: "123456",
        // 兰州
        // username: "cgxfadmin",
        // password: "123456",
        // 攀枝花
        // username: "pzhadmin",
        // password: "123456",
        username: "",
        password: "",
        code:''
      },

      loginRules: {
        username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
        code: [{ required: true, trigger: "blur", message: "请输入验证码" },{
          min: 4, max: 6, trigger: "blur", message: "请正确输入验证码" 
        }],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" },
          { min: 6, max: 18, trigger: "blur", message: "密码长度为6到18位" },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      imgUrl:'',
      key:'',
      titleName:''
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created () {

    this.getTitle()
  },
  mounted() {
    this.particlesInit()
    this.refreshCode()
  },

  methods: {
    refreshCode(){
      this.$api.get('/index/common/verify').then(res=>{
      if (res.code==200) {
        this.key = res.key
        this.imgUrl = res.image
      }
    })
    },
    particlesInit() {
      particlesJS("particles-js", particlesConfig);
    },
    showPassword() {
      this.passwordType = this.passwordType === "password" ? "" : "password";
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          let loginForm = this.loginForm;
          this.loading = true;
          loginForm.key = this.key
          // 兰州授权
          //   this.$store.dispatch("user/auth",{appid:"lzcgqxf",appkey:"123456"}).then((res) => {
          // 攀枝花授权

          this.$store.dispatch("user/auth", { appid: "scpzhxf", appkey: "123456" }).then((res) => {
            this.$store.dispatch("user/login", loginForm).then(() => {

              this.loading = false;
              this.$router.push({ path: this.redirect || "/" });
            }).catch(() => {
              this.loading = false;
            });
          })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
    getTitle(){
      var currentDomain = window.location.origin;
  
      this.$api.get(`/index/common/index?domain=${currentDomain}`).then(res=>{
        this.titleName=res.data.name
      })
    }
  },
};
</script>

<style lang="scss" scoped>
.particles-js-box {
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 0;
}
:deep(.el-form-item__content) {
    display: flex;
    justify-content: space-between;
    .codeimg {
        cursor: pointer;
    }
}
#particles-js {
    background-color: #002060;
    width: 100%;
    height: 100%;
}
[data-mode="mobile"] {
    #login-box {
        max-width: 80%;
        .login-banner {
            display: none;
        }
    }
}
.index-a {
    position: absolute;
    bottom: 2%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: rgb(182, 180, 180);
    text-decoration: none;
}
:deep(input[type="password"]::-ms-reveal) {
    display: none;
}
:deep(.i18n-selector) {
    position: absolute;
    top: 20px;
    right: 20px;
    cursor: pointer;
    font-size: 18px;
    color: #fff;
    mix-blend-mode: difference;
}
.bg-banner {
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    // background-image: url(../assets/images/login-bg.jpg);
    // background-size: cover;
    // background-position: center center;
}
.login__particles {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
}
#login-box {
    display: flex;
    justify-content: space-between;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    background: rgba(255, 255, 255, 0);
    border-radius: 10px;
    overflow: hidden;
    // box-shadow: 0 0 5px #999;
    .login-banner {
        width: 300px;
        background-image: url(../assets/images/loginLogo.png);
        background-size: 80% 80%;
        background-repeat: no-repeat;
        background-position: center 40px;
    }
    .login-form {
        width: 450px;
        padding: 50px 35px 30px;
        overflow: hidden;
        .svg-icon {
            vertical-align: -0.35em;
        }
        .title-container {
            position: relative;
            .title {
                font-size: 22px;
                color: #fff;
                margin: 0 auto 30px;
                text-align: center;
                font-weight: bold;
                @include text-overflow;
            }
        }
    }
    :deep(.el-input) {
        height: 48px;
        line-height: inherit;
        width: 100%;
        input {
            height: 48px;
        }
        .el-input__prefix {
            left: 10px;
        }
        .el-input__suffix {
            right: 10px;
        }
    }
    .flex-bar {
        display: flex;
        justify-content: space-between;
    }
    .el-checkbox {
        margin: 20px 0;
    }
}
.copyright {
    position: absolute;
    bottom: 30px;
    width: 100%;
    margin: 0;
    mix-blend-mode: difference;
}
</style>
